<template>
  <transition name="down">
    <div class="message" :style="style[type]" v-show="visible">
      <el-icon class="i">
        <template v-if="type === 'warn'">
          <Warning class="i" style="width: 16px"/>
        </template>
        <template v-if="type === 'error'">
          <CircleClose class="i" style="width: 16px"/>
        </template>
        <template v-if="type === 'success'">
          <CircleCheck class="i" style="width: 16px"/>
        </template>
      </el-icon>
      <span class="text">{{ text }}</span>
    </div>
  </transition>
</template>

<script setup lang='ts' >
import { Warning, CircleClose, CircleCheck } from '@element-plus/icons-vue'

const props = withDefaults(defineProps<{
  type: string,
  text: string
}>(), {
  type: 'warn',
  text: ''
})
// 定义一个对象，包含三种情况的样式，对象key就是类型字符串
const style = {
  warn: {
    icon: 'Warning',
    color: '#E6A23C',
    backgroundColor: 'rgb(253, 246, 236)',
    borderColor: 'rgb(250, 236, 216)'
  },
  error: {
    icon: 'CircleClose',
    color: '#F56C6C',
    backgroundColor: 'rgb(254, 240, 240)',
    borderColor: 'rgb(253, 226, 226)'
  },
  success: {
    icon: 'CircleCheck',
    color: '#67C23A',
    backgroundColor: 'rgb(240, 249, 235)',
    borderColor: 'rgb(225, 243, 216)'
  }
}

// 控制元素显示/隐藏
const visible = ref(false)
onMounted(() => {
  visible.value = true
})

</script>

<style lang="scss" scoped>
.message {
  width: 300px;
  height: 50px;
  position: fixed;
  z-index: 9999;
  left: 50%;
  margin-left: -150px;
  top: 25px;
  line-height: 50px;
  padding: 0 25px;
  border: 1px solid #e4e4e4;
  background: #f5f5f5;
  color: #999;
  border-radius: 4px;
  /* :deep(.i) */.i {
    margin-right: 4px;
    vertical-align: middle;
    display: inline-block;
  }
  .text {
    vertical-align: middle;
  }
}
.down {
  &-enter {
    &-from {
      transform: translate3d(0,-75px,0);
      opacity: 0;
    }
    &-active {
      transition: all 0.5s;
    }
    &-to {
      transform: none;
      opacity: 1;
    }
  }
  &-leave {
    &-from {
      transform: none;
      opacity: 1;
    }
    &-active {
      transition: all 0.5s;
    }
    &-to {
      transform: translate3d(0,-75px,0);
      opacity: 0;
    }

  }
}
</style>
